package com.stn.zjx.givingtreasure.fragment.search;

import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hhl.library.FlowTagLayout;
import com.hhl.library.OnTagSelectListener;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.DropItemListAdapter;
import com.stn.zjx.givingtreasure.adapter.RightSelectTagAdapter;
import com.stn.zjx.givingtreasure.adapter.ShopItemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseSearchResultFragment;
import com.stn.zjx.givingtreasure.demo.DropItem;
import com.stn.zjx.givingtreasure.demo.ShopShow;
import com.stn.zjx.givingtreasure.event.DropSelectEvent;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ShopSearchFragment extends BaseSearchResultFragment {



    @BindView(R.id.multiple_order_tv)
    TextView multipleOrderTv;
    @BindView(R.id.multiple_order_iv)
    ImageView multipleOrderIv;
    @BindView(R.id.multiple_order_bt)
    RelativeLayout multipleOrderBt;
    @BindView(R.id.visitor_order_tv)
    TextView visitorOrderTv;
    @BindView(R.id.visitor_order_bt)
    RelativeLayout visitorOrderBt;
    @BindView(R.id.send_order_tv)
    TextView sendOrderTv;
    @BindView(R.id.send_order_up_iv)
    ImageView sendOrderUpIv;
    @BindView(R.id.send_order_down_iv)
    ImageView sendOrderDownIv;
    @BindView(R.id.send_order_bt)
    RelativeLayout sendOrderBt;
    @BindView(R.id.screen_bt)
    RelativeLayout screenBt;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.drop_rv)
    RecyclerView dropRv;
    @BindView(R.id.under_select_layer)
    RelativeLayout underSelectLayer;
    @BindView(R.id.under_select)
    LinearLayout underSelect;
    @BindView(R.id.screen_right_head)
    View screenRightHead;
    @BindView(R.id.right_screen_reset_bt)
    TextView rightScreenResetBt;
    @BindView(R.id.right_screen_sure_bt)
    TextView rightScreenSureBt;
    @BindView(R.id.right_screen)
    LinearLayout rightScreen;
    @BindView(R.id.shop_type_flow_layout)
    FlowTagLayout shopTypeFlowLayout;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;

    public ShopItemListAdapter adapter;
    public LinearLayoutManager lm;

    public int multi_pos = 0;
    public int drop_select_pos = 0;
    public int send_pos = 0;
    public DropItemListAdapter drop_adapter;

    private LinearLayoutManager drop_lm;

    private RightSelectTagAdapter shopTypeTagAdapter;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDropSelect(DropSelectEvent event) {
        switch (event.flag) {
            case DropSelectEvent.Drop_Search_Result_Shop_Select:
                multi_pos = event.position;
                DropItem dropItem = new Gson().fromJson(event.data, DropItem.class);
                multipleOrderTv.setText(dropItem.name);
                underSelect.setVisibility(View.GONE);
                multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                break;
        }
    }

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container) {
        if (rootView == null) {
            rootView = View.inflate(activity, R.layout.fragment_search_shop, null);
            ButterKnife.bind(this, rootView);
            initView();
        }
        return rootView;
    }

    @Override
    public void initData() {

        List<DropItem> dropItems = new ArrayList<>();
        DropItem dropItem1 = new DropItem();
        DropItem dropItem2 = new DropItem();
        DropItem dropItem3 = new DropItem();
        dropItem1.orderName = getString(R.string.multiple_order);
        dropItem1.name = getString(R.string.multiple);
        dropItem2.orderName = getString(R.string.distance_order);
        dropItem2.name = getString(R.string.near_far);
        dropItem3.orderName = getString(R.string.distance_far_near_order);
        dropItem3.name = getString(R.string.far_near);
        dropItems.add(dropItem1);
        dropItems.add(dropItem2);
        dropItems.add(dropItem3);
        drop_adapter.pos = multi_pos;
        drop_adapter.refreshList(dropItems);
        underSelectLayer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                underSelect.setVisibility(View.GONE);
            }
        });

        List<ShopShow> shopShows=new ArrayList<>();
         ShopShow s1=new ShopShow();
         s1.setShopLogo("");
          s1.setShopName("FLY HOME 时尚女装");
          s1.setShopLogo("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543756513643&di=b641ef157f3885de8fa98716163b2180&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20170925%2F4f95a0b56b134c3da6f4ef8a85a14de4.jpeg");

        s1.setFollowShopCount(58);
        s1.setRecommand(10.0);
         s1.setCommentStart(4.0);
         s1.setCommentCount(102);
         s1.setAddress("深圳市福田区上沙龙秋村十六巷8-1");
         s1.setAddressNear("160米");
         s1.setIsLiving(0);
         s1.setShowType(2);
         s1.setShopVideos("");
        List<String> imgs1=new ArrayList<>();
           imgs1.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543756967784&di=8f594799480a5c1502885f78a3704f37&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fbaike%2Fc0%253Dbaike60%252C5%252C5%252C60%252C20%253Bt%253Dgif%2Fsign%3D248eb839fbdcd100d991f07313e22c75%2F6a63f6246b600c33980dccfc194c510fd9f9a189.jpg");
         s1.setShopDisplayImgs(imgs1);

           shopShows.add(s1);

        adapter.refreshList(shopShows);


    }

    public void initView() {
        lm = new LinearLayoutManager(activity);
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(lm);
        rv.setAdapter(adapter = new ShopItemListAdapter(activity));


        drop_lm = new LinearLayoutManager(activity);
        drop_lm.setOrientation(LinearLayoutManager.VERTICAL);

        dropRv.setLayoutManager(drop_lm);
        dropRv.setAdapter(drop_adapter = new DropItemListAdapter(activity, DropSelectEvent.Drop_Search_Result_Shop_Select));

        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);


        shopTypeTagAdapter = new RightSelectTagAdapter(activity);
        shopTypeFlowLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI);
        shopTypeFlowLayout.setAdapter(shopTypeTagAdapter);
        shopTypeFlowLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (selectedList.size() == 0) {
                    return;
                }

            }
        });
        List<String> da = new ArrayList<>();
        da.add("生活");
        da.add("服装");
        da.add("美妆护理");
        da.add("健康");
        da.add("电器");
        da.add("数码");
        da.add("餐饮");
        da.add("杂物");
        da.add("网游");
        da.add("工艺");
        da.add("其他");
        shopTypeTagAdapter.onlyAddAll(da);

        shopTypeFlowLayout.clearAllOption();
    }


    @OnClick({R.id.right_screen_reset_bt, R.id.right_screen_sure_bt,
            R.id.multiple_order_bt, R.id.visitor_order_bt, R.id.send_order_bt,
            R.id.screen_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.right_screen_reset_bt:
                shopTypeFlowLayout.clearAllOption();
                break;
            case R.id.right_screen_sure_bt:
                drawerLayout.closeDrawer(Gravity.RIGHT);
                break;
            case R.id.multiple_order_bt:
                switch (drop_select_pos) {
                    case 0:
                        if (underSelect.getVisibility() == View.VISIBLE) {
                            underSelect.setVisibility(View.GONE);
                            multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                            return;
                        }
                        multipleOrderIv.setImageResource(R.mipmap.order_red_up);
                        multipleOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.default_red1));
                        underSelect.setVisibility(View.VISIBLE);
                        break;
                    default:
                        visitorOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                        sendOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                        sendOrderUpIv.setImageResource(R.mipmap.order_black_up);
                        sendOrderDownIv.setImageResource(R.mipmap.order_black_down);
                        multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                        multipleOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.default_red1));
                        drop_select_pos = 0;
                        break;
                }

                break;
            case R.id.visitor_order_bt:
                if (drop_select_pos == 0 && underSelect.getVisibility() == View.VISIBLE) {
                    underSelect.setVisibility(View.GONE);
                    multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                    return;
                }

                sendOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                sendOrderUpIv.setImageResource(R.mipmap.order_black_up);
                sendOrderDownIv.setImageResource(R.mipmap.order_black_down);
                multipleOrderIv.setImageResource(R.mipmap.order_black_down);
                multipleOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                visitorOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.default_red1));
                drop_select_pos = 1;
                break;
            case R.id.send_order_bt:
                if (drop_select_pos == 0 && underSelect.getVisibility() == View.VISIBLE) {
                    underSelect.setVisibility(View.GONE);
                    multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                    return;
                }

                switch (drop_select_pos) {
                    case 2:
                        switch (send_pos) {

                            case 1:
                                send_pos = 2;
                                sendOrderUpIv.setImageResource(R.mipmap.order_black_up);
                                sendOrderDownIv.setImageResource(R.mipmap.order_red_down);
                                break;
                            case 2:
                                send_pos = 1;
                                sendOrderUpIv.setImageResource(R.mipmap.order_red_up);
                                sendOrderDownIv.setImageResource(R.mipmap.order_black_down);
                                break;
                        }
                        break;
                    default:
                        multipleOrderIv.setImageResource(R.mipmap.order_black_down);
                        multipleOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));
                        visitorOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.font_black));

                        send_pos = 1;
                        sendOrderTv.setTextColor(ActivityCompat.getColor(activity, R.color.default_red1));
                        sendOrderUpIv.setImageResource(R.mipmap.order_red_up);
                        sendOrderDownIv.setImageResource(R.mipmap.order_black_down);

                        drop_select_pos = 2;
                        break;
                }
                break;
            case R.id.screen_bt:
                if (drop_select_pos == 0 && underSelect.getVisibility() == View.VISIBLE) {
                    underSelect.setVisibility(View.GONE);
                    multipleOrderIv.setImageResource(R.mipmap.order_red_down);
                    return;
                }
                drawerLayout.openDrawer(Gravity.RIGHT);
                break;
        }
    }



}
